%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     OTP 组件启动工具
%%% @end
%%%-------------------------------------------------------------------
-module(boot_misc).

-export([
    make_child/1
    ,start_child/3
]).

-include("common.hrl").


%%%===================================================================
%%% API functions
%%%===================================================================

make_child(Children) ->
    lists:map(fun
                  ({Id, {M, F, A}, Restart, Shutdown, Type, Modules}) ->
                      {Id, {?MODULE, start_child, [M, F, A]}, Restart, Shutdown, Type, Modules};
                  ({Id, {M, F, A}}) ->
                      {Id, {?MODULE, start_child, [M, F, A]}, transient, 100000, worker, [Id]}
              end, Children).

start_child(M, F, A) ->
    ?ECHO("正在启动[~w]", [M]),
    case erlang:apply(M, F, A) of
        {ok, Pid} when erlang:is_pid(Pid) ->
            ?ECHO("完成启动[~w]", [M]),
            {ok, Pid};
        Err ->
            ?ECHO("启动出错[~w]:~n~p", [M, Err]),
            Err
    end.
